import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GetTableList {
    public  static List<Table> tableList(String path) throws IOException {
        //String path = "D:\\生态环境江北分局信息资源目录 - 副本.xlsx";

        //创建输入流
        FileInputStream fis = new FileInputStream(path);
        //获取工作蒲
        Workbook workbook = new XSSFWorkbook(fis);
        //获取工作表
        Sheet sheet = workbook.getSheet("信息资源");

        List<Table> list = new ArrayList<>();

        //设置个临时变量存放值
        String temp_xxzymc = "temp";
        String temp_xxzygsfl = "temp";
        String temp_xxzygs = "temp";
        String temp_zdlyfl = "temp";
        String temp_ssxtmc = "temp";
        String temp_updateTime = "temp";
        String temp_concreteTime = "temp";

        //循环遍历得到所有的行，并将其存放入List集合中
        for(int i=2;i<sheet.getPhysicalNumberOfRows();i++){
            //获取前几个合并单元格的值
            String xxzymc,xxzygsfl,xxzygs,zdlyfl,ssxtmc,updateTime,concreterTime;
            if (sheet.getRow(i).getCell(0).getStringCellValue().equals("")){
                xxzymc = temp_xxzymc;
                xxzygsfl = temp_xxzygsfl;
                xxzygs = temp_xxzygs;
                zdlyfl = temp_zdlyfl;
                ssxtmc = temp_ssxtmc;
                updateTime = temp_updateTime;
                concreterTime = temp_concreteTime;
            }else {
                xxzymc = sheet.getRow(i).getCell(0).getStringCellValue();
                temp_xxzymc = xxzymc;

                xxzygsfl = sheet.getRow(i).getCell(1).getStringCellValue();
                temp_xxzygsfl = xxzygsfl;

                xxzygs = sheet.getRow(i).getCell(2).getStringCellValue();
                temp_xxzygs = xxzygs;

                zdlyfl = sheet.getRow(i).getCell(3).getStringCellValue();
                temp_zdlyfl = zdlyfl;

                ssxtmc = sheet.getRow(i).getCell(4).getStringCellValue();
                temp_ssxtmc = ssxtmc;

                updateTime = sheet.getRow(i).getCell(5).getStringCellValue();
                temp_updateTime = updateTime;

                concreterTime = sheet.getRow(i).getCell(6).getStringCellValue();
                temp_concreteTime = concreterTime;
            }

            //获取后面几个单元格的值
            String cname = sheet.getRow(i).getCell(7).getStringCellValue();
            String ename = sheet.getRow(i).getCell(8).getStringCellValue();
            String fieldDescription = sheet.getRow(i).getCell(9).getStringCellValue();
            String dataType = sheet.getRow(i).getCell(10).getStringCellValue();

            String dataLength;//判断如果数据长度单元格为空
            if (sheet.getRow(i).getCell(11) == null){
                dataLength = "";
            }else {
                dataLength = sheet.getRow(i).getCell(11).getStringCellValue();
            }
            String isDict = sheet.getRow(i).getCell(12).getStringCellValue();
            String isNull = sheet.getRow(i).getCell(13).getStringCellValue();
            String iskey = sheet.getRow(i).getCell(14).getStringCellValue();
            String defaultValue = sheet.getRow(i).getCell(15).getStringCellValue();
            String shareProperty = sheet.getRow(i).getCell(16).getStringCellValue();

            String shareCondition;
            if(sheet.getRow(i).getCell(17) == null){
                shareCondition = "";
            }else {
                shareCondition = sheet.getRow(i).getCell(17).getStringCellValue();
            }

            String openProperty = sheet.getRow(i).getCell(18).getStringCellValue();
            //将每个单元格的数据添加进Table对象中并存入list集合中
            list.add(new Table(xxzymc,xxzygsfl,xxzygs,zdlyfl,ssxtmc,updateTime,concreterTime,
                    cname,ename,fieldDescription,dataType,dataLength,isDict,isNull,iskey,
                    defaultValue,shareProperty,shareCondition,openProperty));
        }
        //System.out.println(list);
        fis.close();
        return list;

    }

}
